package com.yungui.libary.service;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.yungui.libary.entity.User;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class UserService {
    public static List<User> userList = Lists.newArrayList();
    static {
        User userA = new User();
        userA.setId(1L);
        userA.setUsername("usera");
        userA.setPassword("usera");
        userList.add(userA);
        User userB = new User();
        userB.setId(2L);
        userB.setUsername("userb");
        userB.setPassword("userb");
        userList.add(userB);
    }
    /**
     * 根据userId获取用户
     * @param userId
     * @return
     */
    public User findUserById(Long userId) {
        Map<Long,User> userMap = userList.stream().collect(Collectors.toMap(User::getId,Function.identity()));
        User user = userMap.get(userId);
        return user;
    }

    /**
     * 根据姓名获取用户
     * @param user
     * @return
     */
    public User findByUserName(User user) {
        Map<String,User> userMap = userList.stream().collect(Collectors.toMap(User::getUsername,Function.identity()));
        User userResult = userMap.get(user.getUsername());
        return userResult;
    }
}
